% ******* Make sigle-channel fid *******
% Created : 20 Oct, 2013
% Author  : Sungjin Kim
% Modified : 20 Oct, 2013
% Description : Make single-channel fid from multi-channel file
% ************************************************************************               

function multi2singleCh(filename, nCh)
    %% read header information.
    c_arg={3};
    [~, headerinfo] = readfidn(filename, 1, 1, c_arg);
    nblocks=headerinfo{1, 1}(1); % header info
    ntraces=headerinfo{1, 1}(2); % status info
    blockPerCh = nblocks/nCh;

    %% store the fid into cell array.
    complexFID=cell(nblocks, ntraces);
    for ix_trace=1:1:ntraces
        for ix_block=1:1:nblocks
            [complexFID{ix_block, ix_trace}, ~] = readfidn(filename, ix_block, ix_trace, c_arg);
        end
    end
    
    %% Write a fid file
    for ix_ch = 1:1:nCh
        %file open 
        filename_w = [filename num2str(ix_ch)];       
        fid = fopen(filename_w, 'w', 'b');
        
        % write file header.
        fwrite(fid, blockPerCh, 'int32');           % **number of blocks in file 
        fwrite(fid, headerinfo{1, 1}(2), 'int32');  % number of traces per block
        fwrite(fid, headerinfo{1, 1}(3), 'int32');  % number of elements per trace 
        fwrite(fid, headerinfo{1, 1}(4), 'int32');  % number of bytes per element
        fwrite(fid, headerinfo{1, 1}(5), 'int32');  %  number of bytes per trace
        fwrite(fid, headerinfo{1, 1}(6), 'int32');  % number of bytes per block
        fwrite(fid, headerinfo{1, 1}(7), 'int16');  % software version, file_id status bits
        fwrite(fid, headerinfo{1, 1}(8), 'int16');  % status of whole file
        fwrite(fid, headerinfo{1, 1}(9), 'int32');  % number of block headers per block
        
        % write block header
        unitblock = 0;
        for ix_block = ix_ch:4:nblocks*ntraces
            unitblock = unitblock + 1;
          
            if unitblock > blockPerCh
                break;
            end
            
            % write block header
            fwrite(fid, headerinfo{2, 1}(1), 'int16'); % scaling factor
            fwrite(fid, headerinfo{2, 1}(2), 'int16'); % status of data in block
            fwrite(fid, unitblock, 'int16');             % **block index
            fwrite(fid, headerinfo{2, 1}(4), 'int16'); % mode of data in block
            fwrite(fid, headerinfo{2, 1}(5), 'int32'); % ct value for FID
            fwrite(fid, headerinfo{2, 1}(6), 'int32'); % f2 (2D-f1) left phase in phasefile
            fwrite(fid, headerinfo{2, 1}(7), 'int32'); % f2 (2D-f1) right phase in phasefile
            fwrite(fid, headerinfo{2, 1}(8), 'int32'); % level drift correction
            fwrite(fid, headerinfo{2, 1}(9), 'int32'); % tilt drift correction
            
            fprintf('Block %d is written\n', unitblock);
            
            for ix_trace = 1:1:ntraces
                % write data parts
                for idx1 = 1 : 1: headerinfo{1, 1}(3)/2
                    fwrite(fid, [real(complexFID{ix_block, ix_trace}(idx1)), imag(complexFID{ix_block, ix_trace}(idx1))], 'float32');
                end
%                 fprintf('\tTrace %d is written\n', ix_trace);
            end
        end

        % fid close
        fclose(fid);
        fprintf('\t\tChannel %d is written\n', ix_ch);
        
    end
    
    fprintf('******The number of row and column in matrix : %d by %d******\n', ntraces, nblocks/nCh);
    fprintf('**************The separation has been done!!****************\n');
    
    return;
end % end of function